using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
using WebPart=System.Web.UI.WebControls.WebParts.WebPart;
using Microsoft.SharePoint.WebControls;

namespace zevenseas.Community.Web.UI.WebControls.WebParts
{
    public class DateTimeFilter : WebPart, IWebEditable
    {
        private string listId = "";
        private string dateTimeFieldId = "";
        private string viewId = "";

        private SPGridView gridView;

        
        [WebBrowsable(false), WebDisplayName("List Display Name")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("DateTime Settings")]
        [DefaultValue("")]
        public string ListId
        {
            get { return listId; }
            set
            {
                listId = value;
            }
        }

        [WebBrowsable(false), WebDisplayName("DateTime Property Display Name")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("DateTime Settings")]
        [DefaultValue("")]
        public string DateTimeFieldId
        {
            get { return dateTimeFieldId; }
            set
            {
                dateTimeFieldId = value;
            }
        }

        [WebBrowsable(false), WebDisplayName("DateTime Property Display Name")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("DateTime Settings")]
        [DefaultValue("")]
        public string ViewId
        {
            get { return viewId; }
            set
            {
                viewId = value;
            }
        }

        
        protected override void CreateChildControls()
        {
            gridView = new SPGridView();
            
            SPFieldDateTime dateTimeField;
            SPList list;
            SPView view;

            try
            {
                list = SPContext.Current.Web.Lists[new Guid(listId)];
                dateTimeField = (SPFieldDateTime)list.Fields[new Guid(DateTimeFieldId)];
                view = list.Views[new Guid(viewId)];

                
            }
            catch (System.Exception)
            {
                Literal configWebPart = new Literal();
                configWebPart.Text = "To configure this web part, <a href=" + GetToolPaneLink() +
                                     ">open the tool pane</a> and then select the List and Choice Column";                
                Controls.Add(configWebPart);
                return;
            }

            //todo : Add the query CAML to the original query

            Label listRender = new Label();
            listRender.Text = view.RenderAsHtml();
            this.Controls.Add(listRender);
                    
            base.CreateChildControls();
        }

        protected SPGridView PopulateGrid(SPGridView gridView, SPView view)
        {
            System.Collections.Specialized.StringCollection strCollection = view.ViewFields.ToStringCollection();
            
            

            for (int i = 0; i < strCollection.Count; i++)
            {
                try
                {
                    if (view.ParentList.Fields.GetFieldByInternalName(strCollection[i]).Title != null)
                    {
                        SPField field = view.ParentList.Fields.GetFieldByInternalName(strCollection[i]);
                        
                        SPBoundField column = new SPBoundField();
                        column.HeaderText = field.Title.ToString();
                        column.DataField = field.Title.ToString();
                        gridView.Columns.Add(column);
                    }
                }
                catch (Exception error)
                {
                    //writer.Write(error.Message.ToString());
                }
            }

            SPQuery viewQuery = new SPQuery();
            viewQuery.Query = view.Query;

            gridView.DataSource = view.ParentList.GetItems(viewQuery);
            gridView.DataBind();

            
            return gridView;

        }

        protected string GetToolPaneLink()
        {
            bool hasRights = ((ISecurableObject)SPContext.Current.Web).DoesUserHavePermissions((SPBasePermissions)Microsoft.SharePoint.SPPermissionGroup.WebDesigner);
             if (!hasRights)
                return "Web Part has not been configured";

            string editLink = ToolPane.GetShowExtensibleToolPaneEvent(string.Format(@"'{0}'", this.UniqueID));
            editLink = string.Format(@"javascript:{0}", editLink.Replace(@"ExtensibleView", @"Edit"));
            return editLink;
        } 


        #region IWebEditable Members

        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> editors = new List<EditorPart>();

            EditorPart dateTimeColumnEditor = new DateTimeFilterEditor();
            dateTimeColumnEditor.ID = "DateTimeColumnEditor" + this.ID;

            editors.Add(dateTimeColumnEditor);
            return new EditorPartCollection(editors);
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }

        #endregion
    }
}